<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" destroyOnClose showFooter title="提交审核" :width="700" @ok="handleSubmit">
    <BasicForm @register="registerForm"></BasicForm>
  </BasicDrawer>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import { BasicDrawer, useDrawerInner } from '@/components/drawer';
import { BasicForm, useForm } from '@/components/form/index';
import { Api, getUserInfo } from '../data/user.api';
import { waitCheckFormSchema } from '../data/user.data';
// 声明Emits
const emit = defineEmits(['success', 'register']);
//是否是编辑模式
const isUpdate = ref(true);
//配置表单
const [registerForm, { resetFields, setFieldsValue, validate, saveOrUpdate }] = useForm({
  rowKey: 'userId',
  updateApi: Api.setWaitCheck,
  schemas: waitCheckFormSchema,
});
//表单赋值
const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
  console.info(data);
  setDrawerProps({ confirmLoading: false });
  await resetFields();
  isUpdate.value = !!data?.isUpdate;
  if (isUpdate.value) {
    let record = data.record;
    let userId = record.userId;
    let result = await getUserInfo({ userId: userId });
    setFieldsValue(result);
  }
});

/**
 * 表单提交
 */
async function handleSubmit() {
  try {
    setDrawerProps({ confirmLoading: true });
    await saveOrUpdate(emit);
    closeDrawer();
  } finally {
    setDrawerProps({ confirmLoading: false });
  }
}
</script>
